<?php
namespace app\api\controller;
use x\Xbase;
use x\my\pay\Alipay;
use x\my\Pay;
class Notice extends Xbase
{
    /**
     * 微信充值通知
     * @return [type] [description]
     */
    public function wxrecharge(){
        $config = [
          'mode'   => 'optional',
        ];
        $pay = Pay::wechat($config);
        $result = $pay->verify();

        #更新订单
        $rs = $this->db->query('select * from recharge where order_no=?',[$result->out_trade_no],1);
        $pay_order_no = $result->transaction_id;
        $this->db->begin();
        $mysql = [];
        $d = [
            'pay_status'=>1,
            'pay_order_no'=>$pay_order_no,
            'pay_data' => '',
            'update_time'=>time()
        ];
        $mysql[] = $this->db->update('recharge',$d,['id=? and pay_status=?', [$rs['id'], 0]]);
        #插入账单
        $mysql[] = m('Userbill', 'xcom')->doit($result->out_trade_no, $rs['user_id'], 100, 'recharge', $rs['id'],  $rs['money'],'money', '充值',$rs['fee']);
        $res = $this->db->end($mysql);
        if($res){
          //处理购买产品的订单
          if ($rs['tab']=='order_goods') {
              $order_nos = explode(',',$rs['tab_order_no']);
              foreach ($order_nos as $val) {
                  $result = s('OrderGoods','c')->pay($val);
              }
              $this->db->update('recharge',['id'=>$rs['id'],'is_count'=>1]);
          }else if($rs['tab']=='user_vip'){
              s("UserVip", "c")->pay($rs["tab_order_no"]);
              $this->db->update('recharge',['id'=>$rs['id'],'is_count'=>1]);
          }else if($rs['tab']=='user_upgrade'){
              s("UserUpgrade", "c")->pay($rs["tab_order_no"]);
              $this->db->update('recharge',['id'=>$rs['id'],'is_count'=>1]);
          }
          $pay->success()->send();
        }
    }

    //废弃
    public function payCallBack(){
        $pay_type = $this->get('pay_type');
        switch ($pay_type) {
            case 0:
                $app = Weixin::getPayApp();
                $response = $app->handlePaidNotify(function($message, $fail){
                    $rs = $this->db->query('select * from recharge where order_no=?',[$message['out_trade_no']],1);
                    if (!$rs) {
                        exit($fail('Order not exist.'));
                    }elseif($rs['pay_status']!=0){
                        err('已支付');
                    }
                    if($message['result_code'] == 'SUCCESS' && $rs["pay_status"] == 0){
                        $pay_order_no = $message['transaction_id'];
                        $this->db->begin();
                        $mysql = [];
                        #更新订单
                        $d = [
                            'id'=>$rs['id'],
                            'pay_status'=>1,
                            'pay_order_no'=>$pay_order_no,
                            'update_time'=>time()
                        ];
                        $mysql[] = $this->db->update('recharge',$d);
                        #插入账单
                        $mysql[] = m('Userbill', 'xcom')->doit($message['out_trade_no'], $rs['user_id'], 100, 'recharge', $rs['id'],  $rs['money'],'money', '充值',$rs['fee']);

                        #绑定关系
                        $mysql[] = m('User','c')->bind($rs['user_id']);

                        $rs = $this->db->end($mysql);
                        if(!$rs){
                            err('支付账单失败');
                        }
                    }else{
                        err('支付错误');
                    }
                });
                exit($response);
                break;

            default:
                $pay = new Alipay();
                $notify = $this->post;
                //验证
                if ($pay->verifyNotify($notify)) {
                    //获取订单信息
                    $info = $pay->getInfo();
                    if ($info['status']) {
                        //成功,处理
                        $order_no = $info["out_trade_no"];
                        //修改订单状态
                        $rs = $this->db->query('select * from recharge where order_no=?',[$order_no],1);
                        $this->db->begin();
                        $mysql = [];
                        #更新订单
                        $d = [
                            'id'=>$rs['id'],
                            'pay_status'=>1,
                            'pay_order_no'=>'',
                            'update_time'=>time()
                        ];
                        $mysql[] = $this->db->update("recharge",$d);
                        #插入账单
                        $mysql[] = m('Userbill', 'xcom')->doit($order_no, $rs['user_id'], 100, 'recharge', $rs['id'],  $rs['real_money'],'money', '充值',$rs['fee']);

                        #绑定关系
                        $mysql[] = m('User','c')->bind($rs['user_id']);

                        $rs = $this->db->end($mysql);
                        if(!$rs){
                            err('支付账单失败');
                        }
                        $pay->notifySuccess();
                    } else {
                        err("支付失败！");
                    }
                } else {
                    exit("Access Denied");
                }
                break;
        }
    }
}
